<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>3.3 输出 | 引言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../c03/s04.html" />
    
    
    <link rel="prev" href="../c03/s02.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.3"
        data-chapter-title="3.3 输出"
        data-filepath="c03/s03.md"
        data-basepath=".."
        data-revision="Fri Feb 10 2017 17:36:30 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        引言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="c01/index.html">
            
                
                    <a href="../c01/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        1 关于Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="c01/s01.html">
            
                
                    <a href="../c01/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        1.1 Tornado是为何物
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="c01/s02.html">
            
                
                    <a href="../c01/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        1.2 Tornado与Django
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="c02/index.html">
            
                
                    <a href="../c02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        2 初识Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="c02/s01.html">
            
                
                    <a href="../c02/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        2.1 安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="c02/s02.html">
            
                
                    <a href="../c02/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        2.2 Hello Itcast
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="c02/s03.html">
            
                
                    <a href="../c02/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        2.3 httpserver
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="c02/s04.html">
            
                
                    <a href="../c02/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        2.4 options
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="c02/s05.html">
            
                
                    <a href="../c02/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        2.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="c03/index.html">
            
                
                    <a href="../c03/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        3 深入Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="c03/s01.html">
            
                
                    <a href="../c03/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        3.1 Application
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="c03/s02.html">
            
                
                    <a href="../c03/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        3.2 输入
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.3" data-path="c03/s03.html">
            
                
                    <a href="../c03/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        3.3 输出
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="c03/s04.html">
            
                
                    <a href="../c03/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        3.4 接口与调用顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="c03/s05.html">
            
                
                    <a href="../c03/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        3.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="c04/index.html">
            
                
                    <a href="../c04/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        4 模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="c04/s01.html">
            
                
                    <a href="../c04/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        4.1 静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="c04/s02.html">
            
                
                    <a href="../c04/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        4.2 使用模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="c04/s03.html">
            
                
                    <a href="../c04/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        4.3 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="c05/index.html">
            
                
                    <a href="../c05/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        5 数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="c05/s01.html">
            
                
                    <a href="../c05/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        5.1 数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="c05/s02.html">
            
                
                    <a href="../c05/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        5.2 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="c06/index.html">
            
                
                    <a href="../c06/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        6 安全应用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="c06/s01.html">
            
                
                    <a href="../c06/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        6.1 Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="c06/s02.html">
            
                
                    <a href="../c06/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        6.2 XSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="c06/s03.html">
            
                
                    <a href="../c06/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        6.3 用户验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="c06/s04.html">
            
                
                    <a href="../c06/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        6.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="c07/index.html">
            
                
                    <a href="../c07/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        7 异步与WebSocket
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="c07/s01.html">
            
                
                    <a href="../c07/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        7.1 认识异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="c07/s02.html">
            
                
                    <a href="../c07/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        7.2 Tornado异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="c07/s03.html">
            
                
                    <a href="../c07/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        7.3 WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="c07/s04.html">
            
                
                    <a href="../c07/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        7.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="c08/index.html">
            
                
                    <a href="../c08/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        8 部署
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="c08/s01.html">
            
                
                    <a href="../c08/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        8.1 部署Tornado
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >引言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="33-&#x8F93;&#x51FA;">3.3 &#x8F93;&#x51FA;</h1>
<h2 id="1-writechunk">1. write(chunk)</h2>
<p>&#x5C06;chunk&#x6570;&#x636E;&#x5199;&#x5230;&#x8F93;&#x51FA;&#x7F13;&#x51B2;&#x533A;&#x3002;&#x5982;&#x6211;&#x4EEC;&#x5728;&#x4E4B;&#x524D;&#x7684;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x4E2D;&#x5199;&#x7684;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(<span class="hljs-string">&quot;hello itcast!&quot;</span>)
</code></pre>
<h4 id="&#x60F3;&#x4E00;&#x60F3;&#xFF0C;&#x53EF;&#x4E0D;&#x53EF;&#x4EE5;&#x5728;&#x540C;&#x4E00;&#x4E2A;&#x5904;&#x7406;&#x65B9;&#x6CD5;&#x4E2D;&#x591A;&#x6B21;&#x4F7F;&#x7528;write&#x65B9;&#x6CD5;&#xFF1F;">&#x60F3;&#x4E00;&#x60F3;&#xFF0C;&#x53EF;&#x4E0D;&#x53EF;&#x4EE5;&#x5728;&#x540C;&#x4E00;&#x4E2A;&#x5904;&#x7406;&#x65B9;&#x6CD5;&#x4E2D;&#x591A;&#x6B21;&#x4F7F;&#x7528;write&#x65B9;&#x6CD5;&#xFF1F;</h4>
<p><strong>&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x51FA;&#x73B0;&#x4EC0;&#x4E48;&#x6548;&#x679C;&#xFF1F;</strong></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(<span class="hljs-string">&quot;hello itcast 1!&quot;</span>)
        self.write(<span class="hljs-string">&quot;hello itcast 2!&quot;</span>)
        self.write(<span class="hljs-string">&quot;hello itcast 3!&quot;</span>)
</code></pre>
<blockquote>
<p>write&#x65B9;&#x6CD5;&#x662F;&#x5199;&#x5230;&#x7F13;&#x51B2;&#x533A;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x50CF;&#x5199;&#x6587;&#x4EF6;&#x4E00;&#x6837;&#x591A;&#x6B21;&#x4F7F;&#x7528;write&#x65B9;&#x6CD5;&#x4E0D;&#x65AD;&#x8FFD;&#x52A0;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#xFF0C;&#x6700;&#x7EC8;&#x6240;&#x6709;&#x5199;&#x5230;&#x7F13;&#x51B2;&#x533A;&#x7684;&#x5185;&#x5BB9;&#x4E00;&#x8D77;&#x4F5C;&#x4E3A;&#x672C;&#x6B21;&#x8BF7;&#x6C42;&#x7684;&#x54CD;&#x5E94;&#x8F93;&#x51FA;&#x3002;</p>
</blockquote>
<h4 id="&#x60F3;&#x4E00;&#x60F3;&#xFF0C;&#x5982;&#x4F55;&#x5229;&#x7528;write&#x65B9;&#x6CD5;&#x5199;json&#x6570;&#x636E;&#xFF1F;">&#x60F3;&#x4E00;&#x60F3;&#xFF0C;&#x5982;&#x4F55;&#x5229;&#x7528;write&#x65B9;&#x6CD5;&#x5199;json&#x6570;&#x636E;&#xFF1F;</h4>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        stu = {
            <span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;zhangsan&quot;</span>,
            <span class="hljs-string">&quot;age&quot;</span>:<span class="hljs-number">24</span>,
            <span class="hljs-string">&quot;gender&quot;</span>:<span class="hljs-number">1</span>,
        }
        stu_json = json.dumps(stu)
        self.write(stu_json)
</code></pre>
<p><strong>&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4E0D;&#x7528;&#x81EA;&#x5DF1;&#x624B;&#x52A8;&#x53BB;&#x505A;json&#x5E8F;&#x5217;&#x5316;&#xFF0C;&#x5F53;write&#x65B9;&#x6CD5;&#x68C0;&#x6D4B;&#x5230;&#x6211;&#x4EEC;&#x4F20;&#x5165;&#x7684;chunk&#x53C2;&#x6570;&#x662F;&#x5B57;&#x5178;&#x7C7B;&#x578B;&#x540E;&#xFF0C;&#x4F1A;&#x81EA;&#x52A8;&#x5E2E;&#x6211;&#x4EEC;&#x8F6C;&#x6362;&#x4E3A;json&#x5B57;&#x7B26;&#x4E32;&#x3002;</strong></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        stu = {
            <span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;zhangsan&quot;</span>,
            <span class="hljs-string">&quot;age&quot;</span>:<span class="hljs-number">24</span>,
            <span class="hljs-string">&quot;gender&quot;</span>:<span class="hljs-number">1</span>,
        }
        self.write(stu)
</code></pre>
<h4 id="&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x6709;&#x4EC0;&#x4E48;&#x5DEE;&#x5F02;&#xFF1F;">&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x6709;&#x4EC0;&#x4E48;&#x5DEE;&#x5F02;&#xFF1F;</h4>
<p>&#x5BF9;&#x6BD4;&#x4E00;&#x4E0B;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x7684;&#x54CD;&#x5E94;&#x5934;header&#x4E2D;<code>Content-Type</code>&#x5B57;&#x6BB5;&#xFF0C;&#x81EA;&#x5DF1;&#x624B;&#x52A8;&#x5E8F;&#x5217;&#x5316;&#x65F6;&#x4E3A;<code>Content-Type:text/html; charset=UTF-8</code>&#xFF0C;&#x800C;&#x91C7;&#x7528;write&#x65B9;&#x6CD5;&#x65F6;&#x4E3A;<code>Content-Type:application/json; charset=UTF-8</code>&#x3002;</p>
<p><strong>write&#x65B9;&#x6CD5;&#x9664;&#x4E86;&#x5E2E;&#x6211;&#x4EEC;&#x5C06;&#x5B57;&#x5178;&#x8F6C;&#x6362;&#x4E3A;json&#x5B57;&#x7B26;&#x4E32;&#x4E4B;&#x5916;&#xFF0C;&#x8FD8;&#x5E2E;&#x6211;&#x4EEC;&#x5C06;<code>Content-Type</code>&#x8BBE;&#x7F6E;&#x4E3A;<code>application/json; charset=UTF-8</code>&#x3002;</strong></p>
<h2 id="2-setheadername-value">2. set_header(name, value)</h2>
<p>&#x5229;&#x7528;set_header(name, value)&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x624B;&#x52A8;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;name&#x3001;&#x503C;&#x4E3A;value&#x7684;&#x54CD;&#x5E94;&#x5934;header&#x5B57;&#x6BB5;&#x3002;</p>
<p>&#x7528;set_header&#x65B9;&#x6CD5;&#x6765;&#x5B8C;&#x6210;&#x4E0A;&#x9762;write&#x6240;&#x505A;&#x7684;&#x5DE5;&#x4F5C;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        stu = {
            <span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;zhangsan&quot;</span>,
            <span class="hljs-string">&quot;age&quot;</span>:<span class="hljs-number">24</span>,
            <span class="hljs-string">&quot;gender&quot;</span>:<span class="hljs-number">1</span>,
        }
        stu_json = json.dumps(stu)
        self.write(stu_json)
        self.set_header(<span class="hljs-string">&quot;Content-Type&quot;</span>, <span class="hljs-string">&quot;application/json; charset=UTF-8&quot;</span>)
</code></pre>
<h2 id="3-setdefaultheaders">3. set_default_headers()</h2>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x4F1A;&#x5728;&#x8FDB;&#x5165;HTTP&#x5904;&#x7406;&#x65B9;&#x6CD5;&#x524D;&#x5148;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x53EF;&#x4EE5;&#x91CD;&#x5199;&#x6B64;&#x65B9;&#x6CD5;&#x6765;&#x9884;&#x5148;&#x8BBE;&#x7F6E;&#x9ED8;&#x8BA4;&#x7684;headers&#x3002;<strong>&#x6CE8;&#x610F;&#xFF1A;&#x5728;HTTP&#x5904;&#x7406;&#x65B9;&#x6CD5;&#x4E2D;&#x4F7F;&#x7528;set_header()&#x65B9;&#x6CD5;&#x4F1A;&#x8986;&#x76D6;&#x6389;&#x5728;set_default_headers()&#x65B9;&#x6CD5;&#x4E2D;&#x8BBE;&#x7F6E;&#x7684;&#x540C;&#x540D;header&#x3002;</strong></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_default_headers</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x6267;&#x884C;&#x4E86;set_default_headers()&quot;</span>
        <span class="hljs-comment"># &#x8BBE;&#x7F6E;get&#x4E0E;post&#x65B9;&#x5F0F;&#x7684;&#x9ED8;&#x8BA4;&#x54CD;&#x5E94;&#x4F53;&#x683C;&#x5F0F;&#x4E3A;json</span>
        self.set_header(<span class="hljs-string">&quot;Content-Type&quot;</span>, <span class="hljs-string">&quot;application/json; charset=UTF-8&quot;</span>)
        <span class="hljs-comment"># &#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;itcast&#x3001;&#x503C;&#x4E3A;python&#x7684;header</span>
        self.set_header(<span class="hljs-string">&quot;itcast&quot;</span>, <span class="hljs-string">&quot;python&quot;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x6267;&#x884C;&#x4E86;get()&quot;</span>
        stu = {
            <span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;zhangsan&quot;</span>,
            <span class="hljs-string">&quot;age&quot;</span>:<span class="hljs-number">24</span>,
            <span class="hljs-string">&quot;gender&quot;</span>:<span class="hljs-number">1</span>,
        }
        stu_json = json.dumps(stu)
        self.write(stu_json)
        self.set_header(<span class="hljs-string">&quot;itcast&quot;</span>, <span class="hljs-string">&quot;i love python&quot;</span>) <span class="hljs-comment"># &#x6CE8;&#x610F;&#x6B64;&#x5904;&#x91CD;&#x5199;&#x4E86;header&#x4E2D;&#x7684;itcast&#x5B57;&#x6BB5;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x6267;&#x884C;&#x4E86;post()&quot;</span>
        stu = {
            <span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;zhangsan&quot;</span>,
            <span class="hljs-string">&quot;age&quot;</span>:<span class="hljs-number">24</span>,
            <span class="hljs-string">&quot;gender&quot;</span>:<span class="hljs-number">1</span>,
        }
        stu_json = json.dumps(stu)
        self.write(stu_json)
</code></pre>
<p>&#x7EC8;&#x7AEF;&#x4E2D;&#x6253;&#x5370;&#x51FA;&#x7684;&#x6267;&#x884C;&#x987A;&#x5E8F;&#xFF1A;</p>
<p><img src="../images/set_defaulta_headers_sequence.png" alt="&#x6267;&#x884C;&#x987A;&#x5E8F;"></p>
<p>get&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x7684;&#x54CD;&#x5E94;header:</p>
<p><img src="../images/set_defaulta_headers_get.png" alt="get&#x65B9;&#x5F0F;"></p>
<p>post&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x7684;&#x54CD;&#x5E94;header:</p>
<p><img src="../images/set_defaulta_headers_post.png" alt="post&#x65B9;&#x5F0F;"></p>
<h2 id="4-setstatusstatuscode-reasonnone">4. set_status(status_code, reason=None)</h2>
<p>&#x4E3A;&#x54CD;&#x5E94;&#x8BBE;&#x7F6E;&#x72B6;&#x6001;&#x7801;&#x3002;</p>
<p>&#x53C2;&#x6570;&#x8BF4;&#x660E;&#xFF1A;</p>
<ul>
<li>status_code int&#x7C7B;&#x578B;&#xFF0C;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x82E5;reason&#x4E3A;None&#xFF0C;&#x5219;&#x72B6;&#x6001;&#x7801;&#x5FC5;&#x987B;&#x4E3A;&#x4E0B;&#x8868;&#x4E2D;&#x7684;&#x3002;</li>
<li>reason string&#x7C7B;&#x578B;&#xFF0C;&#x63CF;&#x8FF0;&#x72B6;&#x6001;&#x7801;&#x7684;&#x8BCD;&#x7EC4;&#xFF0C;&#x82E5;&#x4E3A;None&#xFF0C;&#x5219;&#x4F1A;&#x88AB;&#x81EA;&#x52A8;&#x586B;&#x5145;&#x4E3A;&#x4E0B;&#x8868;&#x4E2D;&#x7684;&#x5185;&#x5BB9;&#x3002;</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">Code</th>
<th style="text-align:left">Enum Name</th>
<th style="text-align:left">Details</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">100</td>
<td style="text-align:left">CONTINUE</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.2.1</td>
</tr>
<tr>
<td style="text-align:center">101</td>
<td style="text-align:left">SWITCHING_PROTOCOLS</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.2.2</td>
</tr>
<tr>
<td style="text-align:center">102</td>
<td style="text-align:left">PROCESSING</td>
<td style="text-align:left">WebDAV RFC 2518, Section 10.1</td>
</tr>
<tr>
<td style="text-align:center">200</td>
<td style="text-align:left">OK</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.3.1</td>
</tr>
<tr>
<td style="text-align:center">201</td>
<td style="text-align:left">CREATED</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.3.2</td>
</tr>
<tr>
<td style="text-align:center">202</td>
<td style="text-align:left">ACCEPTED</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.3.3</td>
</tr>
<tr>
<td style="text-align:center">203</td>
<td style="text-align:left">NON_AUTHORITATIVE_INFORMATION</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.3.4</td>
</tr>
<tr>
<td style="text-align:center">204</td>
<td style="text-align:left">NO_CONTENT</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.3.5</td>
</tr>
<tr>
<td style="text-align:center">205</td>
<td style="text-align:left">RESET_CONTENT</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.3.6</td>
</tr>
<tr>
<td style="text-align:center">206</td>
<td style="text-align:left">PARTIAL_CONTENT</td>
<td style="text-align:left">HTTP/1.1 RFC 7233, Section 4.1</td>
</tr>
<tr>
<td style="text-align:center">207</td>
<td style="text-align:left">MULTI_STATUS</td>
<td style="text-align:left">WebDAV RFC 4918, Section 11.1</td>
</tr>
<tr>
<td style="text-align:center">208</td>
<td style="text-align:left">ALREADY_REPORTED</td>
<td style="text-align:left">WebDAV Binding Extensions RFC 5842, Section 7.1 (Experimental)</td>
</tr>
<tr>
<td style="text-align:center">226</td>
<td style="text-align:left">IM_USED</td>
<td style="text-align:left">Delta Encoding in HTTP RFC 3229, Section 10.4.1</td>
</tr>
<tr>
<td style="text-align:center">300</td>
<td style="text-align:left">MULTIPLE_CHOICES</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.4.1</td>
</tr>
<tr>
<td style="text-align:center">301</td>
<td style="text-align:left">MOVED_PERMANENTLY</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.4.2</td>
</tr>
<tr>
<td style="text-align:center">302</td>
<td style="text-align:left">FOUND</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.4.3</td>
</tr>
<tr>
<td style="text-align:center">303</td>
<td style="text-align:left">SEE_OTHER</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.4.4</td>
</tr>
<tr>
<td style="text-align:center">304</td>
<td style="text-align:left">NOT_MODIFIED</td>
<td style="text-align:left">HTTP/1.1 RFC 7232, Section 4.1</td>
</tr>
<tr>
<td style="text-align:center">305</td>
<td style="text-align:left">USE_PROXY</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.4.5</td>
</tr>
<tr>
<td style="text-align:center">307</td>
<td style="text-align:left">TEMPORARY_REDIRECT</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.4.7</td>
</tr>
<tr>
<td style="text-align:center">308</td>
<td style="text-align:left">PERMANENT_REDIRECT</td>
<td style="text-align:left">Permanent Redirect RFC 7238, Section 3 (Experimental)</td>
</tr>
<tr>
<td style="text-align:center">400</td>
<td style="text-align:left">BAD_REQUEST</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.1</td>
</tr>
<tr>
<td style="text-align:center">401</td>
<td style="text-align:left">UNAUTHORIZED</td>
<td style="text-align:left">HTTP/1.1 Authentication RFC 7235, Section 3.1</td>
</tr>
<tr>
<td style="text-align:center">402</td>
<td style="text-align:left">PAYMENT_REQUIRED</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.2</td>
</tr>
<tr>
<td style="text-align:center">403</td>
<td style="text-align:left">FORBIDDEN</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.3</td>
</tr>
<tr>
<td style="text-align:center">404</td>
<td style="text-align:left">NOT_FOUND</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.4</td>
</tr>
<tr>
<td style="text-align:center">405</td>
<td style="text-align:left">METHOD_NOT_ALLOWED</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.5</td>
</tr>
<tr>
<td style="text-align:center">406</td>
<td style="text-align:left">NOT_ACCEPTABLE</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.6</td>
</tr>
<tr>
<td style="text-align:center">407</td>
<td style="text-align:left">PROXY_AUTHENTICATION_REQUIRED</td>
<td style="text-align:left">HTTP/1.1 Authentication RFC 7235, Section 3.2</td>
</tr>
<tr>
<td style="text-align:center">408</td>
<td style="text-align:left">REQUEST_TIMEOUT</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.7</td>
</tr>
<tr>
<td style="text-align:center">409</td>
<td style="text-align:left">CONFLICT</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.8</td>
</tr>
<tr>
<td style="text-align:center">410</td>
<td style="text-align:left">GONE</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.9</td>
</tr>
<tr>
<td style="text-align:center">411</td>
<td style="text-align:left">LENGTH_REQUIRED</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.10</td>
</tr>
<tr>
<td style="text-align:center">412</td>
<td style="text-align:left">PRECONDITION_FAILED</td>
<td style="text-align:left">HTTP/1.1 RFC 7232, Section 4.2</td>
</tr>
<tr>
<td style="text-align:center">413</td>
<td style="text-align:left">REQUEST_ENTITY_TOO_LARGE</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.11</td>
</tr>
<tr>
<td style="text-align:center">414</td>
<td style="text-align:left">REQUEST_URI_TOO_LONG</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.12</td>
</tr>
<tr>
<td style="text-align:center">415</td>
<td style="text-align:left">UNSUPPORTED_MEDIA_TYPE</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.13</td>
</tr>
<tr>
<td style="text-align:center">416</td>
<td style="text-align:left">REQUEST_RANGE_NOT_SATISFIABLE</td>
<td style="text-align:left">HTTP/1.1 Range Requests RFC 7233, Section 4.4</td>
</tr>
<tr>
<td style="text-align:center">417</td>
<td style="text-align:left">EXPECTATION_FAILED</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.14</td>
</tr>
<tr>
<td style="text-align:center">422</td>
<td style="text-align:left">UNPROCESSABLE_ENTITY</td>
<td style="text-align:left">WebDAV RFC 4918, Section 11.2</td>
</tr>
<tr>
<td style="text-align:center">423</td>
<td style="text-align:left">LOCKED</td>
<td style="text-align:left">WebDAV RFC 4918, Section 11.3</td>
</tr>
<tr>
<td style="text-align:center">424</td>
<td style="text-align:left">FAILED_DEPENDENCY</td>
<td style="text-align:left">WebDAV RFC 4918, Section 11.4</td>
</tr>
<tr>
<td style="text-align:center">426</td>
<td style="text-align:left">UPGRADE_REQUIRED</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.5.15</td>
</tr>
<tr>
<td style="text-align:center">428</td>
<td style="text-align:left">PRECONDITION_REQUIRED</td>
<td style="text-align:left">Additional HTTP Status Codes RFC 6585</td>
</tr>
<tr>
<td style="text-align:center">429</td>
<td style="text-align:left">TOO_MANY_REQUESTS</td>
<td style="text-align:left">Additional HTTP Status Codes RFC 6585</td>
</tr>
<tr>
<td style="text-align:center">431</td>
<td style="text-align:left">REQUEST_HEADER_FIELDS_TOO_LARGE Additional</td>
<td style="text-align:left">HTTP Status Codes RFC 6585</td>
</tr>
<tr>
<td style="text-align:center">500</td>
<td style="text-align:left">INTERNAL_SERVER_ERROR</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.6.1</td>
</tr>
<tr>
<td style="text-align:center">501</td>
<td style="text-align:left">NOT_IMPLEMENTED</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.6.2</td>
</tr>
<tr>
<td style="text-align:center">502</td>
<td style="text-align:left">BAD_GATEWAY</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.6.3</td>
</tr>
<tr>
<td style="text-align:center">503</td>
<td style="text-align:left">SERVICE_UNAVAILABLE</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.6.4</td>
</tr>
<tr>
<td style="text-align:center">504</td>
<td style="text-align:left">GATEWAY_TIMEOUT</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.6.5</td>
</tr>
<tr>
<td style="text-align:center">505</td>
<td style="text-align:left">HTTP_VERSION_NOT_SUPPORTED</td>
<td style="text-align:left">HTTP/1.1 RFC 7231, Section 6.6.6</td>
</tr>
<tr>
<td style="text-align:center">506</td>
<td style="text-align:left">VARIANT_ALSO_NEGOTIATES</td>
<td style="text-align:left">Transparent Content Negotiation in HTTP RFC 2295, Section 8.1 (Experimental)</td>
</tr>
<tr>
<td style="text-align:center">507</td>
<td style="text-align:left">INSUFFICIENT_STORAGE</td>
<td style="text-align:left">WebDAV RFC 4918, Section 11.5</td>
</tr>
<tr>
<td style="text-align:center">508</td>
<td style="text-align:left">LOOP_DETECTED</td>
<td style="text-align:left">WebDAV Binding Extensions RFC 5842, Section 7.2 (Experimental)</td>
</tr>
<tr>
<td style="text-align:center">510</td>
<td style="text-align:left">NOT_EXTENDED</td>
<td style="text-align:left">An HTTP Extension Framework RFC 2774, Section 7 (Experimental)</td>
</tr>
<tr>
<td style="text-align:center">511</td>
<td style="text-align:left">NETWORK_AUTHENTICATION_REQUIRED</td>
<td style="text-align:left">Additional HTTP Status Codes RFC 6585, Section 6</td>
</tr>
</tbody>
</table>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Err404Handler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x5BF9;&#x5E94;/err/404&quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(<span class="hljs-string">&quot;hello itcast&quot;</span>)
        self.set_status(<span class="hljs-number">404</span>) <span class="hljs-comment"># &#x6807;&#x51C6;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x4E0D;&#x7528;&#x8BBE;&#x7F6E;reason</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Err210Handler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x5BF9;&#x5E94;/err/210&quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(<span class="hljs-string">&quot;hello itcast&quot;</span>)
        self.set_status(<span class="hljs-number">210</span>, <span class="hljs-string">&quot;itcast error&quot;</span>) <span class="hljs-comment"># &#x975E;&#x6807;&#x51C6;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x8BBE;&#x7F6E;&#x4E86;reason</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Err211Handler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x5BF9;&#x5E94;/err/211&quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(<span class="hljs-string">&quot;hello itcast&quot;</span>)
        self.set_status(<span class="hljs-number">211</span>) <span class="hljs-comment"># &#x975E;&#x6807;&#x51C6;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x672A;&#x8BBE;&#x7F6E;reason&#xFF0C;&#x9519;&#x8BEF;</span>
</code></pre>
<p><img src="../images/set_status_404.png" alt="404&#x72B6;&#x6001;&#x7801;&#x6F14;&#x793A;"></p>
<p><img src="../images/set_status_210.png" alt="210&#x72B6;&#x6001;&#x7801;&#x6F14;&#x793A;"></p>
<p><img src="../images/set_status_211.png" alt="211&#x72B6;&#x6001;&#x7801;&#x6F14;&#x793A;"></p>
<h2 id="5-redirecturl">5. redirect(url)</h2>
<p>&#x544A;&#x77E5;&#x6D4F;&#x89C8;&#x5668;&#x8DF3;&#x8F6C;&#x5230;url&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x5BF9;&#x5E94;/&quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(<span class="hljs-string">&quot;&#x4E3B;&#x9875;&quot;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LoginHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x5BF9;&#x5E94;/login&quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(<span class="hljs-string">&apos;&lt;form method=&quot;post&quot;&gt;&lt;input type=&quot;submit&quot; value=&quot;&#x767B;&#x9646;&quot;&gt;&lt;/form&gt;&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self)</span>:</span>
        self.redirect(<span class="hljs-string">&quot;/&quot;</span>)
</code></pre>
<h2 id="6-senderrorstatuscode500-kwargs">6. send_error(status_code=500, **kwargs)</h2>
<p>&#x629B;&#x51FA;HTTP&#x9519;&#x8BEF;&#x72B6;&#x6001;&#x7801;status_code&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;500&#xFF0C;kwargs&#x4E3A;&#x53EF;&#x53D8;&#x547D;&#x540D;&#x53C2;&#x6570;&#x3002;&#x4F7F;&#x7528;send_error&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x540E;tornado&#x4F1A;&#x8C03;&#x7528;write_error()&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x7ED9;&#x6D4F;&#x89C8;&#x5668;&#x5904;&#x7406;&#x540E;&#x7684;&#x9519;&#x8BEF;&#x9875;&#x9762;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(<span class="hljs-string">&quot;&#x4E3B;&#x9875;&quot;</span>)
        self.send_error(<span class="hljs-number">404</span>, content=<span class="hljs-string">&quot;&#x51FA;&#x73B0;404&#x9519;&#x8BEF;&quot;</span>)
</code></pre>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;&#x9ED8;&#x8BA4;&#x7684;<code>write\_error()</code>&#x65B9;&#x6CD5;&#x4E0D;&#x4F1A;&#x5904;&#x7406;<code>send\_error</code>&#x629B;&#x51FA;&#x7684;kwargs&#x53C2;&#x6570;&#xFF0C;&#x5373;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4E2D;<code>content=&quot;&#x51FA;&#x73B0;404&#x9519;&#x8BEF;&quot;</code>&#x662F;&#x6CA1;&#x6709;&#x610F;&#x4E49;&#x7684;&#x3002;</strong></p>
<h4 id="&#x5C1D;&#x8BD5;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x51FA;&#x73B0;&#x4EC0;&#x4E48;&#x95EE;&#x9898;&#xFF1F;">&#x5C1D;&#x8BD5;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x51FA;&#x73B0;&#x4EC0;&#x4E48;&#x95EE;&#x9898;&#xFF1F;</h4>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(<span class="hljs-string">&quot;&#x4E3B;&#x9875;&quot;</span>)
        self.send_error(<span class="hljs-number">404</span>, content=<span class="hljs-string">&quot;&#x51FA;&#x73B0;404&#x9519;&#x8BEF;&quot;</span>)
        self.write(<span class="hljs-string">&quot;&#x7ED3;&#x675F;&quot;</span>) <span class="hljs-comment"># &#x6211;&#x4EEC;&#x5728;send_error&#x518D;&#x6B21;&#x5411;&#x8F93;&#x51FA;&#x7F13;&#x51B2;&#x533A;&#x5199;&#x5185;&#x5BB9;</span>
</code></pre>
<p><img src="../images/send_error_write.png" alt="&#x51FA;&#x9519;&#x4FE1;&#x606F;"></p>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;&#x4F7F;&#x7528;send_error()&#x65B9;&#x6CD5;&#x540E;&#x5C31;&#x4E0D;&#x8981;&#x518D;&#x5411;&#x8F93;&#x51FA;&#x7F13;&#x51B2;&#x533A;&#x5199;&#x5185;&#x5BB9;&#x4E86;&#xFF01;</strong></p>
<h2 id="7-writeerrorstatuscode-kwargs">7. write_error(status_code, **kwargs)</h2>
<p>&#x7528;&#x6765;&#x5904;&#x7406;send_error&#x629B;&#x51FA;&#x7684;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x5E76;&#x8FD4;&#x56DE;&#x7ED9;&#x6D4F;&#x89C8;&#x5668;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x9875;&#x9762;&#x3002;&#x53EF;&#x4EE5;&#x91CD;&#x5199;&#x6B64;&#x65B9;&#x6CD5;&#x6765;&#x5B9A;&#x5236;&#x81EA;&#x5DF1;&#x7684;&#x9519;&#x8BEF;&#x663E;&#x793A;&#x9875;&#x9762;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        err_code = self.get_argument(<span class="hljs-string">&quot;code&quot;</span>, <span class="hljs-keyword">None</span>) <span class="hljs-comment"># &#x6CE8;&#x610F;&#x8FD4;&#x56DE;&#x7684;&#x662F;unicode&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4E0B;&#x540C;</span>
        err_title = self.get_argument(<span class="hljs-string">&quot;title&quot;</span>, <span class="hljs-string">&quot;&quot;</span>)
        err_content = self.get_argument(<span class="hljs-string">&quot;content&quot;</span>, <span class="hljs-string">&quot;&quot;</span>)
        <span class="hljs-keyword">if</span> err_code:
            self.send_error(err_code, title=err_title, content=err_content)
        <span class="hljs-keyword">else</span>:
            self.write(<span class="hljs-string">&quot;&#x4E3B;&#x9875;&quot;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_error</span><span class="hljs-params">(self, status_code, **kwargs)</span>:</span>
        self.write(<span class="hljs-string">u&quot;&lt;h1&gt;&#x51FA;&#x9519;&#x4E86;&#xFF0C;&#x7A0B;&#x5E8F;&#x5458;GG&#x6B63;&#x5728;&#x8D76;&#x8FC7;&#x6765;&#xFF01;&lt;/h1&gt;&quot;</span>)
        self.write(<span class="hljs-string">u&quot;&lt;p&gt;&#x9519;&#x8BEF;&#x540D;&#xFF1A;%s&lt;/p&gt;&quot;</span> % kwargs[<span class="hljs-string">&quot;title&quot;</span>])
        self.write(<span class="hljs-string">u&quot;&lt;p&gt;&#x9519;&#x8BEF;&#x8BE6;&#x60C5;&#xFF1A;%s&lt;/p&gt;&quot;</span> % kwargs[<span class="hljs-string">&quot;content&quot;</span>])
</code></pre>
<p><img src="../images/write_error.png" alt="&#x5C55;&#x793A;&#x9519;&#x8BEF;&#x4FE1;&#x606F;"></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../c03/s02.html" class="navigation navigation-prev " aria-label="Previous page: 3.2 输入"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../c03/s04.html" class="navigation navigation-next " aria-label="Next page: 3.4 接口与调用顺序"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
